package com.allen.study.domain.service;

import com.allen.study.common.base.DomainResponse;
import com.allen.study.domain.entity.EmployeeInfo;
import com.allen.study.domain.repository.IEmployeeInfoRepo;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;

import javax.validation.constraints.NotNull;

/**
 * 领域服务
 *
 * @author AllenSun
 * @since 2025-02-26 22:43
 */
@Service
@AllArgsConstructor
public class EmployeeInfoService {

    /**
     * 资源库
     */
    private final IEmployeeInfoRepo employeeInfoRepo;

    /**
     * 创建
     *
     * @param employeeInfo 实体
     * @return 响应结果
     */
    public DomainResponse<Void> create(@NotNull EmployeeInfo employeeInfo) {
        // 获取当前登录用户
        // SecurityUser securityUser = SecurityUserHelper.get();
        // 设置租户
        // employeeInfo.setTenantId(securityUser.getTenantId());

        // 保存
        employeeInfoRepo.create(employeeInfo);
        return DomainResponse.ok();
    }

    /**
     * 根据主键删除
     *
     * @param employeeInfoId 主键
     * @return 响应结果
     */
    public DomainResponse<Void> deleteById(@NotNull String employeeInfoId) {
        // 根据主键查询
        EmployeeInfo employeeInfo = employeeInfoRepo.queryById(employeeInfoId);
        // 数据存在且租户正确，删除数据
        if (ObjectUtils.isNotEmpty(employeeInfo) && employeeInfo.checkTenant()) {
            employeeInfoRepo.deleteById(employeeInfoId);
        }
        return DomainResponse.ok();
    }

    /**
     * 根据主键更新
     *
     * @param employeeInfo 实体
     * @return 响应结果
     */
    public DomainResponse<Void> updateById(@NotNull EmployeeInfo employeeInfo) {
        // 根据主键查询
        EmployeeInfo exists = employeeInfoRepo.queryById(employeeInfo.getId());
        // 数据存在且租户正确，更新数据
        if (ObjectUtils.isNotEmpty(exists) && exists.checkTenant()) {
            employeeInfoRepo.updateById(employeeInfo);
        }
        return DomainResponse.ok();
    }

    /**
     * 根据主键查询
     *
     * @param employeeInfoId 主键
     * @return 响应结果
     */
    public DomainResponse<EmployeeInfo> queryById(@NotNull String employeeInfoId) {
        EmployeeInfo employeeInfo = employeeInfoRepo.queryById(employeeInfoId);
        return DomainResponse.ok(employeeInfo);
    }
}
